package com.musictab.test.Decryption;

import java.io.File;
import java.io.IOException;

import org.springframework.util.FileCopyUtils;

//解密
public class DecodeDecryptionUtil {

	//计算总共改了多少个文件
	int count = 0;

	public DecodeDecryptionUtil() {

	}

	/**
	 * 改变文件名
	 */
	public int renameFile(String filepath, String regex) {

		File[] fileArray = ReadFileUtils.readfile(filepath, regex);

		for (File file : fileArray) {

			if (file.isDirectory()) {
				//递归
				renameFile(file.getAbsolutePath(), regex);

			} else {

				//获取路径
				String rootPath = file.getParent();

				String oldFileStr = file.getName();

				//文件名
				String oldFileName = oldFileStr.substring(0, oldFileStr.lastIndexOf("."));

				//文件类型
				//String oldFileType = oldFileStr.substring(oldFileStr.lastIndexOf(".") + 1, oldFileStr.length());

				//临时文件
				File newFile = new File(rootPath + File.separator + oldFileName);
				
				file.renameTo(newFile);

				count++;
			}
		}

		return count;
	}

	public static void main(String[] args) {

		DecodeDecryptionUtil d = new DecodeDecryptionUtil();

		System.out.println(d.renameFile("D:\\workspace\\spring\\spring", "\\w+|(\\w+\\.java\\.dat$)"));

	}
}
